package org.agony2d.view.core 
{
	import flash.display.DisplayObject;

	import org.agony2d.core.agony_internal;
	use namespace agony_internal;

final public class OriginSprite extends AgonySprite
{
	
	public function OriginSprite()
	{
		m_x = m_y = m_originX = m_originY = 0
	}
	
	override public function get x() : Number { return m_x - m_originX; }
	override public function set x( v:Number ) :void { super.x = m_x = v + m_originX }
	
	override public function get y() : Number { return m_y - m_originY }
	override public function set y( v:Number ) :void { super.y = m_y = v + m_originY }
	
	final agony_internal function setOrigin( ox:Number, oy:Number ) : void
	{
		var tx:Number, ty:Number
		
		tx = this.x
		ty = this.y
		m_originX = ox
		m_originY = oy
		super.x = m_x = tx + m_originX
		super.y = m_y = ty + m_originY
		m_child.x = -m_originX
		m_child.y = -m_originY
	}
	
	final override agony_internal function dispose() : void
	{
		super.dispose()
		m_child = null
		super.x = super.y = m_x = m_y = m_originX = m_originY = 0
		cachedOriginSpriteList[cachedOriginSpriteLength++] = this
	}
	
	agony_internal static function getOriginSprite( child:DisplayObject ) : OriginSprite
	{
		var origin:OriginSprite
		
		origin = (cachedOriginSpriteLength > 0 ? cachedOriginSpriteLength-- : 0) ? cachedOriginSpriteList.pop() : new OriginSprite()
		origin.m_child = child
		origin.addChild(child)
		return origin
	}
	
	agony_internal static var cachedOriginSpriteList:Array = []
	agony_internal static var cachedOriginSpriteLength:int
	
	agony_internal var m_x:Number, m_y:Number, m_originX:Number, m_originY:Number
	agony_internal var m_child:DisplayObject
}
}